/*

Graph implementation-file 
created by psych0der on 13th arch 2011
copyright (c) psych0der all rights reserved

*/
#include <iostream>
#include <fstream>
#include "Graph.h"

using namespace std;
int stack[20];
int stkp=-1;

int qps,qpe; // queue start and end pointer
int queue[20];


void push(int a)
{
	stkp++;
	stack[stkp] = a;	
}

int pop(void)
{
	if(stkp==-1)
		return stkp;
	else
	{
	int r = stack[stkp];
	stkp--;
	return r;
    }
}

void enque(int a)
{
	if(qpe==-1)
		{
		
			qps=qpe=0;
			queue[qpe] = a;
		}
	else
		qpe++;
	queue[qpe]=a;
}

int deque(void)
{
	
/*	cout<<"\n before :\n";
		for(int c = qps ;c< qpe+1;c++)
		cout<<"-"<<queue[c];*/

	if(qpe==-1)
		return -1;
	else
	{
	int ret = queue[qps];
	for(int i = qps ;i< qpe;i++)
	{
		queue[i] = queue[i+1];
	}
	qpe--;
	/*cout<<"\n after :\n";
	for(int d = qps ;d< qpe+1;d++)
	cout<<"-"<<queue[d];*/
	
	return ret;
	}
}





Graph::Graph()
{
	strcpy(names,"ABCDEFG");
	readFile();
	printGraph();
	
}	
void Graph::readFile()
{
	int dummy;
	ifstream file;
	file.open("graph.txt",ios::in);
		for(int i=0;i<7;i++)
		{
			for(int j=0;j<7;j++)
			{
				file>>dummy;
				graph[i][j] = dummy;
			}
		
			
		}
	
		file.close();	
	
}
void Graph::printGraph()
{
	cout<<"\n";
	
	for(int i=0;i<7;i++)
	{
		for(int j=0;j<7;j++)
		{
			
			cout<<" "<<graph[i][j]<<",";
		}
		cout<<"\n";
		
	}
	
	
}
void Graph::dfs()
{
	
	int val;
	push(0);
	val = pop();
	cout<<"\n--";
	while(val>=0)
	{
		cout<<" "<<names[val];
		for(int h=0;h<7;h++)
		{
			if(graph[val][h]>0)
			{
				push(h);
			}
		}
		
		val = pop();
	}
	
	cout<<"\n";	
}

void Graph::bfs()
{
	qps = qpe =-1;	
	
	int val;
	enque(0);
	val = deque();
	cout<<"\n--";
	while(val>=0)
	{
		cout<<" "<<names[val];
		for(int h=0;h<7;h++)
		{
			if(graph[val][h]>0)
			{
				enque(h);
			}
		}
		
		val = deque();
	}
	
	cout<<"\n";
	
}

